package com.echovin.esb.gateway.constants;

/**
 * 自定义返回码
 *
 * @author dym
 */

public enum ErrorCode {
    /**
     * 成功
     */
    OK(0, "success"),
    FAIL(1000, "fail"),


    /**
     * ouath2鉴权错误
     */
    INVALID_TOKEN(2000, "invalid_token"),
    EXPIRED_TOKEN(2007, "expired_token"),

    /**
     * IP LIMIT错误
     */
    ACCESS_DENIED(4030, "access_denied"),
    ACCESS_DENIED_BLACK_LIMITED(4031, "access_denied_black_limited"),
    ACCESS_DENIED_WHITE_LIMITED(4032, "access_denied_white_limited"),

    /**
     * 请求错误
     */
    BAD_REQUEST(4000, "bad_request"),
    PARAMWTER_ERROR(4001, "parameter_error"),
    NOT_FOUND(4004, "not_found"),
    METHOD_NOT_ALLOWED(4005, "method_not_allowed"),
    MEDIA_TYPE_NOT_ACCEPTABLE(4006, "media_type_not_acceptable"),
    RESPONSE_TIME_OUT(4008, "response_time_out"),
    TOO_MANY_REQUESTS(4029, "too_many_requests"),
    THIRD_SERVICE_ERROR(4009, "service_provider_error"),

    /**
     * 系统错误
     */
    ERROR(5000, "error"),
    GATEWAY_TIMEOUT(5004, "gateway_timeout"),
    SERVICE_UNAVAILABLE(5003, "service_unavailable");


    private int code;
    private String message;

    ErrorCode() {
    }

    private ErrorCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ErrorCode getResultEnum(int code) {
        for (ErrorCode type : ErrorCode.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return ERROR;
    }

    public static ErrorCode getResultEnum(String message) {
        for (ErrorCode type : ErrorCode.values()) {
            if (type.getMessage().equals(message)) {
                return type;
            }
        }
        return ERROR;
    }


    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static String getMessage(int code) {
        for (ErrorCode type : ErrorCode.values()) {
            if (type.getCode() == code) {
                return type.getMessage();
            }
        }
        return null;
    }

}
